#!/usr/local/bin/perl
use strict;
package Ball;
use interaction;
use UI;
use file_IO;
use Data::Dumper;

sub new
{
    my $class = shift;
    my $self = {
        x => shift,
        y => shift,
        inv => shift,
    };

    bless $self, $class;
    return $self;
}

sub get_loc
{
        my ($self, $data, $x, $y) = @_;
        if ( $x eq '' )
        {
                $x = $self->{x};
                $y = $self->{y};
        }
        my $res = $data->{location}->{"$x,$y"}->{region};
        return $res if $res;
        return "Unknown";
}

sub can_go
{
        my ($self, $data, $dx, $dy) = @_;
        my $next_region = $self->get_loc($data,$self->{x}+$dx,$self->{y}+$dy);
        my $req = $data->{region}->{$next_region}->{req};
        return 1 if (grep {$_ eq $req} @{$self->{inv}}) or $req eq '';
        &::output("You can't go there now!");
        return 0;
}

sub print_inv
{
        my ($self) = @_;
        my $inv = $self->{inv};
        if ( scalar(@$inv) == 0 )
        {
                &::output("You have nothing.");
                return;
        }
        my @to_output;
        foreach (@$inv)
        {
                $_ =~ s/_/ /g;
                push @to_output, $_;
        }
        &::output("You have: " . join(", ", @to_output));
        return "You have: " . join(", ", @to_output);
}


sub show_map
{
        my ($self, $data) = @_;
        my $l = 14; #length of one cell
        my $size = 5; #number of cells in one line/column
        my ($undsc, $spc) = ("","");
        for ( my $i = 0; $i < $l; $i++ ) { $undsc = $undsc."_"; $spc = $spc . " "; }
        my $lastundsc = "\|";
        for ( my $i = 0; $i < $size; $i++ )
        {
                $lastundsc = $lastundsc . $undsc . "\|";
        }
        $lastundsc = $lastundsc . "\n";
        my $temp = "";
        for ( my $i = 0; $i < $size; $i++ )
        {
                $temp = $temp . " " . $undsc;
        }
        $undsc = $temp . "\n";
        $temp = "\|";
        for ( my $i = 0; $i < $size; $i++ )
        {
                $temp = $temp . $spc . "\|";
        }
        $spc = $temp . "\n";
        print($undsc);
        for ( my $i = ($size-1)/2; $i >= (1-$size)/2; $i-- )
        {
                print $spc;
                print("\|");
                for ( my $j = (1-$size)/2; $j <= ($size-1)/2; $j++ )
                {
                        my $land = $self->get_loc($data, $self->{x}+$j, $self->{y}+$i);
                        print(" ") unless ( length($land) % 2 eq $l % 2 );
                        for ( my $k = 0; 2*$k < $l - length($land)-1; $k++ ){ print " "; }
                        print$land;
                        for ( my $k = 0; 2*$k < $l - length($land)-1; $k++ ){ print " "; }
                        print"\|";
                }
                print("\n");
                print$spc;
                print$lastundsc;
        }
        
}

sub look
{
        my ($self, $data) = @_;
        &::output("What do you want to look at?> ");
        my $in = &::input;
        chomp($in);
        my $here = $self->get_loc($data);
        my @guys = @{$data->{region}->{$here}->{character}};
        for my $guy (@guys)
        {
           $guy=~$in and &::output($data->{character}->{$guy}->{description}) and return 1;
         }

        my @things = $self->{inv};
        for my $thing (@things)
        {
           $thing=~$in and &::output($data->{thing}->{$thing}->{description}) and return 1;
         }

        &::output("What is this $in you speak of?");
        return 0;

}

sub go
{
  my ($self, $data, $from, $to) = @_;

  if($data->{interaction}->{$to}->{location} ne $from) {&::output("What is this $to you speak of?");return}
  if($data->{interaction}->{$to}->{type} ne "place") {&::output("You walk up to $to. Now what?");return}

  &::interaction($to, $self);
  
  return 1
}

sub talkto
{
  my ($self, $data, $from, $to) = @_;

  if($data->{interaction}->{$to}->{location} ne $from) {&::output("What is this $to you speak of?");return}
  if($data->{interaction}->{$to}->{type} eq "place") {&::output("$to remains silent.");return}

  &::interaction($to, $self);
  
  return 1
}

sub saveme
{
  my ($self) = @_;
  &::save("wojak.xml", $self);
  return 1
}


sub loadme
{
  my ($self) = @_;
  my $loaded=&::load("wojak.xml");
  &::debug (Dumper($loaded));
  $self->{y}=$loaded->{y};
  $self->{x}=$loaded->{x};
  $self->{inv}=$loaded->{inv};
  return 1
}



1;
